﻿using System;
using System.Collections.Generic;

namespace Model.EF
{
    public interface IRepository<TEntity>
        where TEntity : IEntity
    {
        TEntity FindById(int ID);
        #region 查询普通实现方案(基于Lambda表达式的Where查询)
        /// <summary>  
        /// 获取所有Entity 
        /// </summary>
        /// <param name="exp">Lambda条件的where</param> 
        /// <returns></returns>
        IEnumerable<TEntity> GetEntities(Func<TEntity, bool> exp);
        /// <summary>
        /// 计算总个数(分页)
        /// </summary>
        /// <param name="exp">Lambda条件的where</param>
        /// <returns></returns>
        int GetEntitiesCount(Func<TEntity, bool> exp);
        /// <summary>
        /// 分页查询(Linq分页方式)
        /// </summary>
        /// <param name="pageNumber">当前页</param>
        /// <param name="pageSize">页码</param>
        /// <param name="orderName">lambda排序名称</param>
        /// <param name="sortOrder">排序(升序or降序)</param>
        /// <param name="exp">lambda查询条件where</param>
        /// <returns></returns>
        IEnumerable<TEntity> GetEntitiesForPaging(int pageNumber, int pageSize, Func<TEntity, string> orderName, string sortOrder, Func<TEntity, bool> exp);
        /// <summary>
        /// 根据条件查找
        /// </summary>
        /// <param name="exp">lambda查询条件where</param>
        /// <returns></returns>
        TEntity GetEntity(Func<TEntity, bool> exp);
        #endregion       

        #region 查询Sql语句外接接口的查询实现
        /// <summary>
        /// 获取所有Entity(立即执行请使用ToList()
        /// </summary>
        /// <param name="commandText">Sql语句</param>
        /// <param name="objParams">可变参数</param>
        /// <returns></returns>
        IEnumerable<TEntity> GetEntities(string commandText);
        /// <summary>
        /// 计算总个数(分页)
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="commandText">>Sql where语句</param>
        /// <returns></returns>
        int GetEntitiesCount(string tableName, string commandText);
        /// <summary>
        /// 计算总个数(分页)
        /// </summary>
        /// <param name="commandText">Sql语句</param>
        /// <param name="objParams">可变参数</param>
        /// <returns></returns>
        int GetEntitiesCount(string commandText);

        /// <summary>
        /// 分页查询(Linq分页方式)
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="pageNumber">当前页</param>
        /// <param name="pageSize">页码</param>
        /// <param name="orderName">lambda排序名称</param>
        /// <param name="sortOrder">排序(升序or降序)</param>
        /// <param name="commandText">Sql语句</param>
        /// <param name="count">总个数</param>
        /// <returns></returns>
        IEnumerable<TEntity> GetEntitiesForPaging(string tableName, int pageNumber, int pageSize, string orderName, string sortOrder, string commandText, out int count);

        /// <summary>
        /// 根据条件查找
        /// </summary>
        /// <param name="commandText">Sql语句</param>
        /// <param name="objParams">可变参数</param>
        /// <returns></returns>
        TEntity GetEntity(string commandText);

        #endregion 

        List<TEntity> FindBy(string where);
        List<TEntity> FindPageList(string where, int pageIndex, int pageSize, out int totalRecord);
        IEnumerable<TEntity> FindAll();
        //IEnumerable<TEntity> FindAll(string query);
        IEnumerable<TEntity>　FindBySpecification(Func<TEntity,　bool>　spec);
        int Add(TEntity entity);
        void Delete(TEntity entity);
        void Update(TEntity entity);
        void Update(TEntity entity, params string[] fileds);

    }
}
